//更多促销活动展示页
//author：王梦阳

var userId = localStorage.getItem("user"); //用户id

let getMore = {
	version: "v1.0",
	getMoreInfo: function() {
		var data = eval('(' + localStorage.dataset + ')');
		console.log(data);
		let htmlContentP = "";
		let itemId = "";
		for (var i = 0; i < data.data.length; i++) {
			itemId = data.data[i].item_id;
			htmlContentP +=
				`<div class="promo"id="${itemId}" >
					<img class="promo-image"  src="${data.data[i].item_img}"/>
					<div class="promo-content">
					<span class="promo-name">${data.data[i].promo_name}</span>
					<span class="promo-des">${data.data[i].promo_description}</span>
					<span class="promo-time">活动时间：${data.data[i].start_date}到${data.data[i].end_date}</span>
					<span class="promo-price">￥${data.data[i].promo_item_price}</span>
					</div>
				</div>`;
		}
		$("#promo-list").html(htmlContentP);
		//点击活动商品，记录一次浏览记录
		for (var j = 0; j < data.data.length; j++) {
			itemId = data.data[j].item_id;
			document.getElementById(itemId).addEventListener('tap', function() {
				$.ajax({
					type: "POST",
					dataType: "json",
					async: false,
					contentType: "application/json",
					url: "http://39.98.158.247:8040/browse/insert?userId=" + userId + "&itemId=" + itemId,

					success: function() {
						console.log("insert success");
					},
					error: function() {
						console.log("request error");
					}
				})
				window.open("search-item.html", "_blank");
				localStorage.status = 0;
				localStorage.id = this.id;
			})
		}
	}
}

window.onload = function() {
	getMore.getMoreInfo();
}
